<?php
namespace app\ice\model;
use think\Model;
use think\Db;
class ScoreLog extends Model {
    protected $pk = 'id';

    public function saveLog($data){
        $res = $this->allowField(true)->save($data);
        return $res;
    }

        public function getList($data,$page,$orll_page)
    {
        $list['row'] = $this->name('scoreLog')
            ->page($page,$orll_page)
            ->where('uid',$data['uid'])
            ->order('id desc')
            ->select();
        $total = $this->name('scoreLog')
            ->where('uid',$data['uid'])
            ->count();
        $list['count'] = ceil($total/$orll_page);
        $list['page'] = $page;
        if(count($list['row']) < 1 ){
            return false;
        }
        foreach ($list['row'] as $key => &$value) {
            $value['showTime'] = date('Y-m-d',$value['addTime']);
            switch ($value['type']) {
                case 1:
                    # code...
                $value['showScore'] = '+'.$value['score'];
                    break;
                case 2:
                    # code...
                $value['showScore'] = '-'.$value['score'];
                    break;
                
                // default:
                //     $value['showScore'] = '+'.$value['score'];
                //     # code...
                //     break;
            }
            $value['showTime'] = date('Y-m-d',$value['addTime']);

        }
        return $list;
    }


    public function getGoodsListData($data){
        $list = $this->table('slzd_sh_goods_category b')
            // ->join('slzd_sh_goods_category b','a.admin_id = b.seller_id')
            ->join('slzd_sh_goods c','c.cat_id = b.id')
            ->field('goods_name,sort,c.is_hot goods_hot,cat_id,goods_id,store_count,click_count,comment_count,original_img,prom_type,shop_price,market_price,c.seller_id')
            ->where('c.cat_id',$data['category_id'])
            ->where('c.seller_id',$data['seller_id'])
            ->where('is_on_sale',1)
            ->order('sort desc','goods_hot desc')
            ->select();
            foreach ($list as $key => &$value) {
                if($value['original_img']){
                    $value['original_img'] = config('shopimg').$value['original_img'];
                }else{
                    $value['original_img'] = config('shopimg').'/public/upload/10971515042539.jpg';
                    // $value['original_img'] = config('shopimg').'/public/upload/1061525313046.jpg';
                }
            }

        $cat_list = $this->table('slzd_sh_cart a')
            ->field('goods_id,goods_num,COUNT(*)  goods_number')
            ->where('a.seller_id',$data['seller_id'])
            ->where('a.user_id',$data['user_id'])
            ->where('goods_num','egt',1)
            ->group('goods_id')
            ->select();

         $goods_ids = [];
         foreach ($cat_list as  $v) {
             $goods_ids[] = $v->data;
         }
         $goods_ids = array_column($goods_ids, 'goods_number','goods_id');
        foreach ($list as $key => &$value) {
            if(array_key_exists($value['goods_id'], $goods_ids)){
                $value['goods_number'] = $goods_ids[$value['goods_id']];
            }else{
                $value['goods_number'] = 0;

            }
            $value['spec'] = get_spec($value['goods_id']);
            // switch ($value['prom_type']) {
            //     case '0':
            //     $value['price'] = $value['shop_price'];
            //         break;
            //     case '1':
            //     $value['price'] = $value['spike_price'];
            //         break;
            //     case '2':
            //     $value['price'] = $value['promotional_price'];
            //         break;
            // }
            //         unset($value['spike_price']);
            //         unset($value['promotional_price']);
        }
        return $list;
    }
    //查询商品是否存在
    public function getSpecGoodsData($data){
        $res =  $this->table('slzd_sh_spec_goods_price')
        ->where('goods_id',$data['goods_id'])
        ->where('item_id',$data['item_id'])->find();
        return $res;
    }
    public function decStoreNum($data){
        $res = $this->table('slzd_sh_spec_goods_price')
        ->where('goods_id',$data['goods_id'])
        ->where('item_id',$data['item_id'])->setDec('store_count',$data['goods_num']);
        return $res;
    }

    public function getReputations($goodsId){
        $field = ['reputation','remark','create_time','haedimgurl','nickname'];
        $res = $this->name('reputation a')
        ->field($field)
        ->join('ig_user_info b','a.uid = b.id')
        ->where('goods_id',$goodsId)
        ->select();
        foreach ($res as $key => &$value) {
            switch ($value['reputation']) {
                case 0:
                    $value['goodReputationStr'] = '差评';
                break;
                case 1:
                    $value['goodReputationStr'] = '中评';
                break;
                case 2:
                    $value['goodReputationStr'] = '好评';
                break;
            }
        }
        return $res;
    }

    //查询商品是否存在
    //查询是否在规格表中添加过
    public function getSpecGoodsData2($data){
        $res =  $this->table('slzd_sh_spec_goods_price')
        ->where('goods_id',$data['goods_id'])
        // ->where('item_id',$data['item_id'])
        ->find();
        return $res;
    }
    /**
     * 秒杀活动
     * @author lxl
     */
    public function getSeckillGoods($data,$page,$orll_page){
        $now_time = time();
        $where = " start_time <= $now_time and end_time >= $now_time ";
        $list['row'] = M('sh_prom_goods')->field('id,title,start_time,end_time,prom_img')
        ->where($where)->page($page,$orll_page)->select();    //查询活动列表        
        $list['page'] = $page;
        $list['count'] = M('sh_prom_goods')->where($where)->count();  // 查询满足要求的总记录数
        return $list;
    }

    //查询商品是否存在
    public function decStoreCount($data){
        $res =  $this->table('slzd_sh_spec_goods_price')
        ->where('goods_id',$data['goods_id'])
        ->where('item_id',$data['item_id'])->setDec('store_count');
        return $res;
    }

    /**
     * 促销活动
     * @author lxl
     */
    public function getPromotionGoods($data){
    // public function getPromotionGoods($data,$page,$orll_page){
        $now_time = time();
        $where = " start_time <= $now_time and end_time >= $now_time ";
        $where .= " and b.seller_id = {$data['seller_id']} ";
        if(isset($data['type'])){
            $where .= " and type = {$data['type']} ";
        }
        $list = $this->name('sh_prom_goods a')
        ->join('slzd_sh_goods b','a.id = b.prom_id')
        ->where($where)
        // ->page($page,$orll_page)
        ->select();   //查询活动列表
        return $list;
    }

    public function getGoodsDetail($data){
        $field = ["id", "pic", "goods_name", "store_count", "weight", "sales_sum", "goods_content",'price','buyNumMax','buyNumber','commissionType','commission','numberGoodReputation','minPrice'] ;
        $goods = $this->name('goods')
            ->where('id',$data['id'])
            ->field($field)
            ->find();
        $goods['pic'] = config('host').$goods['pic'];
        $goods['pics'] = ['pic'=>$goods['pic']];
        $replace = 'src="'.config('host');
        $goods['goods_content'] = preg_replace('/src="/', $replace, $goods['goods_content']);
        return $goods;
}
    public function getSpecGoodsPrice($data){
        $item_id = $data['item_id'];
        $spec_goods_price  = $this->table('slzd_sh_spec_goods_price')->where('goods_id',$data['goods_id'])->field("key,price,store_count,item_id,key_name")->select(); // 规格 对应 价格 库存表
        foreach ($spec_goods_price as $key => &$value) {
                if($item_id != $value['key']){
                    unset($spec_goods_price[$key]);
                }
            }
        $spec_goods_price = array_values($spec_goods_price);
        return $spec_goods_price;
    }

    public function FlashSale()
    {
        return $this->hasOne('FlashSale','id','prom_id');
    }

    public function PromGoods()
    {
        return $this->hasOne('PromGoods','id','prom_id')->cache(true,10);
    }
    public function GroupBuy()
    {
        return $this->hasOne('GroupBuy','id','prom_id');
    }
    public function getDiscountAttr($value, $data)
    {
        if ($data['market_price'] == 0) {
            $discount = 10;
        } else {
            $discount = round($data['shop_price'] / $data['market_price'], 2) * 10;
        }
        return $discount;
    }
}
